import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class rout {

	public static void performNodeRouting(int nodeID, Graphe graphe)
			throws IOException {
		File Fichier = new File("table-" + nodeID + ".data");
		Fichier.createNewFile();
		Path filePath = Paths.get("table-" + nodeID + ".data");
		Charset charset = Charset.forName("UTF-8");
		List<String> lines = new ArrayList<>();
		Node node = graphe.nodesInt.get(nodeID);
		LinkedList<Node> path;
		node.routing[nodeID] = nodeID; 
		int n = node.routing.length;
		int dest;
		for (int i = 0; i < n; i++) {
			if (i != nodeID) {
				path = bfs.findPath(graphe, nodeID, i);
				path.removeFirst();
				Node destination = path.removeFirst();
				dest = destination.ID;
				node.routing[i] = dest;
			} else {
				dest = nodeID;
			}
			lines.add(i + " => " + dest);
		}
		Files.write(filePath, lines, charset, StandardOpenOption.CREATE);

	}

	public static void performNodeIntervalsRouting(int nodeID, Graphe graphe)
			throws IOException {

		File Fichier = new File("table-" + nodeID + ".data");
		Fichier.createNewFile();
		Path filePath = Paths.get("table-" + nodeID + ".data");
		int currentDestination = 0;
		Charset charset = Charset.forName("UTF-8");
		List<String> lines = new ArrayList<>();
		Node node = graphe.nodesInt.get(nodeID);
		LinkedList<Node> path;
		node.routing[nodeID] = nodeID;
		int n = node.routing.length;
		int dest;
		for (int i = 0; i < n; i++) {
			if (i != nodeID) {
				path = bfs.findPath(graphe, nodeID, i);
				path.removeFirst();
				Node destination = path.removeFirst();
				dest = destination.ID;
				node.routing[i] = dest;
			} else {
				dest = nodeID;
			}
			
				lines.add(i + " => " + dest);
			
			currentDestination = dest;
		}
		Files.write(filePath, lines, charset, StandardOpenOption.CREATE);

	}

	public static void performRouting(Graphe graphe, String type)
			throws IOException {
		//System.out.println(graphe.size); //bug
		for (int i = 0; i < graphe.size; i++) {
			if (type.matches("1")) {
				performNodeRouting(i, graphe); 
			} else if (type.matches("2")) {
				performNodeIntervalsRouting(i, graphe);
			} else {

			}
		}

	}

}
